package mh.demo.calcite;

import mh.demo.calcite.utils.ResultSetUtil;
import org.apache.calcite.adapter.jdbc.JdbcSchema;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaPlus;

import java.sql.*;
import java.util.Map;
import java.util.Properties;

public class JdbcDemo {

    public static void main(String[] args) throws SQLException {
        Properties info = new Properties();
        info.setProperty("caseSensitive", "false");
        Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        Map<String, Object> operand = Map.ofEntries(
                Map.entry("jdbcUrl", "jdbc:mysql://localhost:3307/test"),
                Map.entry("jdbcDriver", "com.mysql.cj.jdbc.Driver"),
                Map.entry("jdbcUser", "root"),
                Map.entry("jdbcPassword", "admin")
        );
        Schema schema = JdbcSchema.create(rootSchema, "test", operand);
        rootSchema.add("test", schema);
        String sql = "select * from test.student";
        Statement statement = calciteConnection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println(ResultSetUtil.resultString(resultSet));
        resultSet.close();
        statement.close();
        connection.close();
    }
}
